# -*- coding: utf-8 -*-

from odoo import models, fields, api


class PriceListProductPricelist(models.Model):
    _inherit = 'product.pricelist'

    megre_price_list = fields.Many2one(comodel_name='product.pricelist', string='要匯入的價格表')

    def do_megre_price_list(self):
        res = []
        for line in self.megre_price_list.item_ids:
            update = False
            for row in self.item_ids:
                if row.product_tmpl_id.id == line.product_tmpl_id.id and line.min_quantity == row.min_quantity:
                    res.append([1, row.id, {
                        'import_price': line.import_price,
                        'fixed_price': line.fixed_price
                    }])
                    update = True
                    print '更新' + line.name.encode('utf-8') + str(line.min_quantity)
                    break

            if update is False:
                line.pricelist_id = self.id
                print '新增' + line.name.encode('utf-8') + str(line.min_quantity)

        self.write({
            'item_ids': res
        })

    def import_price_excel(self):
        action = self.env.ref('yifeng_price_list.product_pricelist_item_action').read()[0]
        action['context'] = {'default_pricelist_id': self.id}
        action['domain'] = [('pricelist_id', '=', self.id)]
        return action


class PriceListProductPricelistItem(models.Model):
    _inherit = "product.pricelist.item"

    name = fields.Char(related='product_tmpl_id.name')
    default_code = fields.Char(related='product_tmpl_id.default_code')
    prduct_state = fields.Many2many(comodel_name='product.state', related='product_tmpl_id.product_satae', string='狀態')
    product_type_line = fields.Many2one(comodel_name='product.type.line', related='product_tmpl_id.product_type_line', string='產品種類')
